#include "xmlparser.h"
#include <QFile>
#include <QtXml>
#include <QDomDocument>
#include <QDebug>
#include <QDir>

//#pragma comment (lib, "utils\\xmlParser.lib")
//#include "utils\xmlParser.h"
#include "p0parameter.h"

//#ifdef 1
void testXml()
{
    XmlParser p;
    QString str = "";
    p.getSimCommIP(str);

    int i =0;
    p.getSimCommPort(i);

    p.getP1Peer(211,str, i);
   
//    p.getFirstValue("ab",str);
//    QHash<qint16,P0Paramer> hash;
//    p.getP0Parameter(hash);
    qDebug() << "test!";
    return;

//    QHash<qint16,P0Paramer>::const_iterator it = hash.constBegin();
//    while (it != hash.constEnd())
//    {
//         P0Paramer par = it.value();
//         qDebug() << it.key() << ": " << par.localPort ;
//         ++it;
//    }

    QString peer = "";
    int port = 0;
    p.getP1Peer(211,peer, port);
    qDebug() << peer << port;
}

void testDomXml()
{
    XmlParser p;
    QString str;
//    p.getFirstValue("a",str);
}
//#endif


XmlParser::XmlParser()
{
}

XmlParser::XmlParser(QString _filename)
{
  filename = _filename;
}

XmlParser::~XmlParser()
{
}

#if 0
 #include <QXmlStreamReader>

void  XmlParser::getFirstValue(const QString key, QString &value)
{
    QString filename(".\\config\\example.xml");
    qDebug() << "enter";
     QFile* file = new QFile(filename);
     if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
     {
         qDebug() << "Couldn't open"
                 << filename << "current" << QDir::currentPath();

         return ;
     }

    QXmlStreamReader xml(file);
    while (!xml.atEnd()) {
        QXmlStreamReader::TokenType tt = xml.readNext();
        if(tt == QXmlStreamReader::EndDocument || tt == QXmlStreamReader::Invalid)
                break;
        else if(tt == QXmlStreamReader::StartElement) {
            qDebug() <<xml.name();

        }
     }

    file->close();

}
#endif

void  XmlParser::getSimCommIP(QString &value)
{
    QString filename(".\\config\\Config.xml");
    QFile* file = new QFile(filename);
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Couldn't open"
                << filename << "current" << QDir::currentPath();
        return ;
    }

    QDomDocument doc("mydocument");
    if (!doc.setContent(file))
    {
        return ;
    }
    
    QDomElement docElem = doc.documentElement();
    QDomElement ele = docElem.firstChildElement("CommunicationServer");
    if ( !ele.isNull())
    {
        ele = ele.firstChildElement("SimComm");
    }
    if (!ele.isNull())
    {
        value = ele.text();
    }

    file->close();
    return;
}

void  XmlParser::getSimCommPort(int &value)
{
    QString filename(".\\config\\Config.xml");
    QFile* file = new QFile(filename);
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Couldn't open"
                << filename << "current" << QDir::currentPath();
        return ;
    }

    QDomDocument doc("mydocument");
    if (!doc.setContent(file))
    {
        return ;
    }
    
    QDomElement docElem = doc.documentElement();
    QDomElement ele = docElem.firstChildElement("CommunicationServer");
    if ( !ele.isNull())
    {
        ele = ele.firstChildElement("SimPort");
    }
    if (!ele.isNull())
    {
        value = ele.text().toInt();
    }

    file->close();
    return;
}

class P0Paramer1
{
public:
    P0Paramer1()
    {
        localPeer = "";
        test = QString();
        localPort = 5555;
    }

    qint16 Did;
    qint16 Sid;

    int localPort;
       
    bool bSendByBytes;
    std::string localPeer;
    QString test;
 
};

void XmlParser::getP0Parameter(QHash<qint16,P0Paramer>& hash)
{
    QString filename(".\\config\\address.xml");
    QFile* file = new QFile(filename);
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Couldn't open"
                << filename << "current" << QDir::currentPath();
        return ;
    }

    QDomDocument doc("mydocument");
    if (!doc.setContent(file))
    {
        return ;
    }

    QDomElement docElem = doc.documentElement();

    QDomNodeList nodeList = docElem.elementsByTagName("p2p_address");
    for(int ii = 0;ii < nodeList.count(); ii++)
    {
        QDomElement el = nodeList.at(ii).toElement();
        P0Paramer par;
        QDomNode pEntries = el.firstChild();
        while(!pEntries.isNull()) 
        {
            QDomElement peData = pEntries.toElement();
            QString tagNam = peData.tagName();

            if(tagNam == "Sid") 
                par.Sid = peData.text().toInt();
            else if ( tagNam == "Did")
                par.Did = peData.text().toInt();
            else if ( tagNam == "peer")
                par.peer = peData.text();
                if(tagNam == "localPeer") 
                par.localPeer = peData.text();
            else if ( tagNam == "local")
                par.localPort = peData.text().toInt();
            else if ( tagNam == "remote")
                par.remotePort = peData.text().toInt();
            else if(tagNam == "sizeRecv") 
                par.sizeRecv = peData.text().toInt();
            else if ( tagNam == "sizeSend")
                par.sizeSend = peData.text().toInt();
            else if ( tagNam == "SendByByte")
                par.bSendByBytes = peData.text() != "true" ? false : true;
            else if(tagNam == "type") 
                par.type = peData.text();
                    
            hash.insert(par.Did,par);
            pEntries = pEntries.nextSibling();
        }
    }
    file->close();
 
    //test
//    qDebug() << hash.count();
//    qDebug("%4s : %-10s","id","peer");
//    QHash<qint16,P0Paramer>::const_iterator it = hash.constBegin();
//    while (it != hash.constEnd())
//    {
//         P0Paramer par = it.value();
//         qDebug() << it.key() << ": " << par.peer ;
//         ++it;
//    }
}

void XmlParser::getP1Peer(qint32 id, QString& peer, int& port)
{
    QString filename(".\\config\\address.xml");
    QFile* file = new QFile(filename);
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Couldn't open"
                << filename << "current" << QDir::currentPath();
        return ;
    }

    QDomDocument doc("mydocument");
    if (!doc.setContent(file))
    {
        return ;
    }
    
    QDomElement docElem = doc.documentElement();

    QDomNodeList nodeList = docElem.elementsByTagName("p1_address");
    for(int ii = 0;ii < nodeList.count(); ii++)
    {
        QDomElement el = nodeList.at(ii).toElement();
        QDomElement ele = el.firstChildElement("Did");
        if (!ele.isNull())
        {
            int did = ele.text().toInt();
            if (did == id)
            {
                peer = el.firstChildElement("peer").text();
                port = el.firstChildElement("remote").text().toInt();
                break;
            }
        }
    }
    file->close();
}

